﻿using System;
using System.Windows;
using static Utility.Common;

namespace WallpaperEngine
{
    public partial class App : Application
    {
        Wallpaper wallpaper;
        NotifyIcon notifyIcon;
        public App()
        {
            try
            {
                Init();
            }
            catch (Utility.StartException exception)
            {
                MessageBox.Show($"应用程序初始化失败!QAQ\n请重启程序或联系作者\n出错函数:{exception.Function}\n额外信息:{exception.Message}");
                Environment.Exit(0);
            }
            catch (Exception exception)
            {
                MessageBox.Show($"{exception.Data.ToString()}\n{exception.Message}\n{exception.Source}\n{exception.StackTrace}\n{exception.TargetSite}\n应用程序出现严重错误!\n请将本窗口截图发给作者,谢谢");
                Environment.Exit(0);
            }
        }

        private void Init()
        {
            CheckFile(new string[] { "Utility.dll" });
            IsStart();

            InitializeComponent();
            Configuration.Init();
            wallpaper = new Wallpaper();
            notifyIcon = new NotifyIcon(wallpaper);

            Exit += ((s, ex) =>
            {
                Configuration.Save();
                wallpaper.Close();
                wallpaper.ToOldWallpaper();
                notifyIcon.Close();
                Environment.Exit(0);
            });
        }
    }
}
